package com.library.admin.listener;

import books.borrowing.entity.SubscriberEntity;

import com.library.admin.AdmLibrary;
import com.library.admin.form.UpdateSubscriberAccountForm;
import com.library.admin.service.LibraryService;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.Notification;

public class UpdateSubscriberListener implements ClickListener {
	private static final long serialVersionUID = 5245891339635887834L;

	private SubscriberEntity subscriber;
	private UpdateSubscriberAccountForm subscriberAccountForm;
	
	public UpdateSubscriberListener(SubscriberEntity subscriber, UpdateSubscriberAccountForm subscriberAccountForm) {
		this.subscriber = subscriber;
		this.subscriberAccountForm = subscriberAccountForm;
	}

	public void buttonClick(ClickEvent event) {
		AdmLibrary libraryApplication = (AdmLibrary) subscriberAccountForm.getApplication();
		if (subscriberAccountForm.isValid()) {
			subscriber.setForename(subscriberAccountForm.getSurnameTextField().getValue().toString());
			subscriber.setName(subscriberAccountForm.getNameTextField().getValue().toString());
			subscriber.setLogin(subscriberAccountForm.getLoginTextField().getValue().toString());
			LibraryService.getServiceSubscriber().update(subscriber);
			libraryApplication.getMainWindow().showNotification("Les informations de l'utilisateur '" + subscriber.getLogin() + "' ont étés modifiées avec succès ",
					Notification.TYPE_HUMANIZED_MESSAGE);
		}
		else {
			libraryApplication.getMainWindow().showNotification("Veuillez remplir tous les champs",
					Notification.TYPE_WARNING_MESSAGE);
		}
	}
}
